<?php

namespace App\Http\Resources\Personal;

use App\Http\Resources\BaseResource;
use Illuminate\Http\Request;

/**
 * 菜单响应资源
 */
class MenuResource extends BaseResource
{
    protected static function newCollection($resource)
    {
        return new MenuCollection($resource, static::class);
    }

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'component' => $this->component,
            'meta' => array_merge(((array) $this->meta) ?? [], [
                'order' => $this->sort,
                'title' => $this->title,
            ]),
            'name' => $this->name,
            'path' => $this->path,
            'redirect' => $this->children->isEmpty() ? '' : $this->children->first()->path,
            'children' => MenuResource::collection($this->children),
        ];
    }
}
